#ifndef __BENCH_H__
#define __BENCH_H__
#include <thread>
#include <string>
#include "Ssslog.hpp"
#include "Logger.hpp"
namespace ssslog
{
    // 开始测试
    void bench(const std::string& name, size_t thread_num, size_t meslen, size_t mes_count)
    {
        std::string str = "hello word";
        Logger::ptr lp =GetLogger(name);
        if(lp.get() == nullptr) return;
        std::string mes("hello word");
        std::cout << "测试日志" << mes_count << "条，总大小" << meslen * mes_count <<"*kb"<< std::endl;
        std::vector<std::thread> threads;
        std::vector<double> cost_time(thread_num);
        size_t x = mes_count / thread_num;
        for(int i = 0; i < thread_num; i++)
        {
            threads.emplace_back([&,i](){
            auto start = std::chrono::high_resolution_clock::now();
            for(size_t j = 0; j < x; j++) 
            {
                lp->fatal("%s", mes.c_str());
            }
            auto end = std::chrono::high_resolution_clock::now();
            auto cost = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
            cost_time[i] = cost.count();
            auto avg = x / cost_time[i];
            std::cout << "线程" << i << "耗时: " << cost.count() << "s" << " 平均：" << (size_t)avg << "/s\n";
            });
        }
        for(auto &thr : threads) 
        {
            thr.join();
        }
        double max_cost = 0;
        for (auto cost : cost_time) max_cost = max_cost < cost ? cost : max_cost;
        std::cout << "总消耗时间: " << max_cost << std::endl;
        std::cout << "平均每秒输出: " << (size_t)(mes_count / max_cost) << "条日志" << std::endl;
        std::cout << "平均每秒输出: " << (size_t)(meslen * mes_count / max_cost / 1024 / 1024) << "MB" << std::endl;
    }
    // 同步测试
    void sync_bench_log(size_t thread_count, size_t mes_count, size_t meslen)
    {
        static int num = 1;
        std::string logger_name = "sync_bench_logger" + std::to_string(num++);
        LOGI("************************************************");
        LOGI("同步日志测试: %d threads, %d messages", thread_count, mes_count);

        ssslog::GlobalLoggerBuilder::ptr lbp(new ssslog::GlobalLoggerBuilder);
        lbp->BuildLoggerName(logger_name);
        lbp->BuildFormatter("%m");
        lbp->BuildSink<ssslog::FileSink>("./logs/sync.log");
        lbp->BuildLoggerType(ssslog::Build::Type::LOGGER_SYNC);
        lbp->build(); 
        ssslog::bench(logger_name, thread_count, meslen, mes_count);
        LOGI("************************************************");
    }
    // 异步测试
    void async_bench_log(size_t thread_count, size_t mes_count, size_t meslen)
    {
        static int num = 1;
        std::string logger_name = "async_bench_logger" + std::to_string(num++);
        LOGI("************************************************");
        LOGI("异步日志测试: %d threads, %d messages", thread_count, mes_count);

        ssslog::GlobalLoggerBuilder::ptr lbp(new ssslog::GlobalLoggerBuilder);
        lbp->BuildLoggerName(logger_name);
        lbp->BuildFormatter("%m");
        lbp->BuildSink<ssslog::FileSink>("./logs/async.log");
        lbp->BuildLoggerType(ssslog::Build::Type::LOGGER_ASYNC);
        lbp->build(); 
        ssslog::bench(logger_name, thread_count, meslen, mes_count);
        LOGI("************************************************");
    }
}

#endif